﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using TCPIPControl.StructHelper;

namespace Common.GeneralClass
{
    public class SpeedDataInfo
    {
    }
   
    public class OperateSpeedDataInfo
    {
        int insertcount = 0;

        MyDb mydb = new MyDb(Mdl_Class.Systemset.DBConnectionString());
        MyDb mydb_insert = new MyDb(Mdl_Class.Systemset.DBConnectionString());

  
        public long AddSpeedDataInfo(SpeedData speedDataInfo)
        {
            long i = 0;
            using (mydb_insert)
            {
                try
                {
                    mydb_insert.Open();
                    Stopwatch sp = new Stopwatch();
                    sp.Start();
                    DateTime nowtime = Convert.ToDateTime(speedDataInfo.Year.ToString() + "-" + speedDataInfo.Month.ToString() + "-" + speedDataInfo.Day.ToString() + " " + speedDataInfo.Hour.ToString() + ":" + speedDataInfo.Minute.ToString() + ":" + speedDataInfo.Second.ToString());
                    string sql = string.Format("insert into T_Rpm(Rpm1,Rpm2,Rpm3,Rpm4,Rpm5,Rpm6,InsertTime,ProjectId) values('{0}','{1}','{2}','{3}','{4}','{5}','{6}','{7}') select @@IDENTITY",
                        speedDataInfo.ChSpeeds[0].rpm, 
                        speedDataInfo.ChSpeeds[1].rpm,
                        speedDataInfo.ChSpeeds[2].rpm,
                        speedDataInfo.ChSpeeds[3].rpm,
                        speedDataInfo.ChSpeeds[4].rpm,
                        speedDataInfo.ChSpeeds[5].rpm,
                        nowtime,
                        Mdl_Class.projectId);
                    i = Convert.ToInt64(mydb_insert.ExecuteScalar(sql));
                    if (i > 0)
                    {
                        Mdl_Class.RpmId = i;
                    }
                
                    insertcount++;
                    sp.Stop();
                    Console.WriteLine("转速 总计耗时:" + sp.ElapsedMilliseconds + " 插入条数：" + insertcount);
                    mydb_insert.Close();
                }
                catch (Exception ex)
                {
                    Mdl_Class.WriteLog.dbErrLog("AddDisplacementDataInfo()", ex.ToString());
                }
            }
            return i;
        }

        /// <summary>
        /// 通过字段名查询
        /// </summary>
        public List<float> GetDataByFieldName(string FieldName, int ProjectId, string dateTime_Start, string dateTime_End, long RpmId)
        {
            List<float> dataslist = new List<float>();
            using (mydb)
            {
                try
                {

                    mydb.Open();
                    string sql = string.Format("select {0} from {1} where 1=1 ", FieldName, "T_Rpm");



                    if (ProjectId > 0)
                    {
                        sql = sql + string.Format(" and projectId={0}", ProjectId);
                    }
                    if (RpmId > 0)
                    {
                        sql = sql + string.Format(" and RpmId={0}", RpmId);
                    }
          

                    if (dateTime_Start != "")
                    {
                        sql = sql + string.Format(" and InsertTime between '{0}' and '{1}'", dateTime_Start, dateTime_End);
                    }

                    sql = sql + " order by Id";
                    mydb.ExecuteReader(sql);
                    while (mydb.DataReader.Read())
                    {
                        float testvalue = 0.0f;
                        if (mydb.DataReader[FieldName] != null)
                        {
                            testvalue = Convert.ToSingle(mydb.DataReader[FieldName].ToString());
                        }
                        dataslist.Add(testvalue);
                    }
                    mydb.Close();
                }
                catch (Exception ex)
                {
                    dataslist.Clear();
                    Mdl_Class.WriteLog.dbErrLog(" OperateSpeedDataInfo.GetDataByFieldName()", ex.ToString());
                }
            }

            return dataslist;
        }
    }
}
